package com.dundun.controller;

import com.dundun.common.utils.Ajax;
import com.dundun.service.impl.OssUploadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "文件上传")
@RequestMapping("/")
@RestController
public class OssUploadController {
    @Resource
    private OssUploadServiceImpl ossUploadService;
    @ApiOperation(value = "上传图片到服务器")
    @PostMapping("/uploadImg")
    public Ajax uploadImg(@RequestParam("file") MultipartFile multipartFile, int userId, String fileName) {
        System.out.println(userId);
        System.out.println(fileName);
        if (userId == 0){
            return Ajax.error();
        }
        if (multipartFile.isEmpty()) {
            System.out.println("空文件");
            return Ajax.error();
        } else {
            String res = ossUploadService.uploadfile(multipartFile);
            return Ajax.success().put("imgUrl",res);
        }
    }
}
